import box/{logger,condit,tools}
shift

[ ${1:-:} == --help ] &&
exec echo -e "用法: update [选项]?\n更新项目的本地仓库\n -f      强制更新\n --help  显示此帮助"

cd $TTB_HOME
Msg "正检查更新"

git remote update

GIT_LOG(){
    git log $1 -n1 --pretty=format:%H
}

GIT_LOGS(){
    Info "最近五次提交:"
    git log -5 --pretty=format:$'\e[35m[%h]\e[0m '"%<($(($(Get-Xterm-Size cols 2>/dev/null||echo 30)-11)),trunc)%s"
}

GIT_REPO_BRANCH=$(git branch --show-current)
GIT_LOCAL=$(GIT_LOG $GIT_REPO_BRANCH)
GIT_ORIGIN=$(GIT_LOG origin/$GIT_REPO_BRANCH)

if In-Value GIT_LOCAL == GIT_ORIGIN;then
    Info "当前项目已是最新"
    exit
else
    Arg=$1
    Choose=-f
    if In-Value Arg == Choose;then
        Msg "正在强制更新..."
        if git fetch --all && git reset --hard origin/$GIT_REPO_BRANCH && git pull;then
            GIT_LOGS
            Info "完成。"
        else
            Err "失败..."
        fi
    else
        Msg "正在更新..."
        if git pull 2>/dev/null;then
            GIT_LOGS
            Info "更新完成"
        else
            Err "更新失败，请尝试强制更新"
        fi
    fi
fi

LOCAL_DIR=$(dirname ${BASH_SOURCE[0]})
source $LOCAL_DIR/setup.sh --